package tuiLayer;

import controlLayer.LoanCtr;
import modelLayer.Loan;
import modelLayer.Copy;
import modelLayer.Person;
import modelLayer.DVD;

import java.util.Scanner;
import java.util.Calendar;
import java.text.SimpleDateFormat;
/**
 * Write a description of class LoanuUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanUI
{
    private LoanCtr loanCtr;
    private DVDUI dvdUI;
    private PersonUI personUI;

    /**
     * Constructor for objects of class LoanuUI
     */
    public LoanUI()
    {
        loanCtr = new LoanCtr();
        dvdUI = new DVDUI();
        personUI = new PersonUI();
    }

    public void createLoan(Copy copy,Person person, String id, String borrowDate, String period, String state)
    {
        loanCtr.createLoan(copy, person, id, borrowDate, period, state);
    }

    public Loan findLoan(String id) 
    {
        return loanCtr.findLoan(id);
    }

    public void updateLoan(Loan loan, String period) 
    {
        loanCtr.updateLoan(loan,period);
    }

    public void deleteLoan(Loan loan) 
    {
        loanCtr.deleteLoan(loan);
    }

    public void registerReturn(Loan loan, String state) 
    {
        loanCtr.registerReturn(loan,state);
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    // Loan menu loop
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeLoansMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = loansAddStart();
            }
            else if(choice == 2)
            {
                print = loansFindStart();
            }
            else if(choice == 3)
            {
                print = loansUpdateStart();
            }
            else if(choice == 4)
            {
                print = loansRemoveStart();

            }
            else if(choice == 5) 
            {
                print = loansReturnStart();

            }
            else if(choice == 6)
            {
                exit = true;
            }

        }
    }

    // Write loan menu
    public void writeLoansMenu(String print) 
    {
        System.out.println("\f *** Loans ***");
        System.out.println(" (1)New");
        System.out.println(" (2)Find");
        System.out.println(" (3)Update");
        System.out.println(" (4)Remove");
        System.out.println(" (5)Register return");
        System.out.println(" (6)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    // Start "Add a loan"
    public String loansAddStart() 
    {

        System.out.println("\nEnter details for the new Loan: ");
        System.out.println("Format: <id> <barcode of the DVD> <serial number of the DVD copy> <cpr of the borrower> <borrow date> <period> <state>");

        String print = null;

        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] inputWords = input.split(" ");

        if(inputWords.length == 7) { //Checking the format
            String id = inputWords[0];
            String dvdBarcode = inputWords[1];
            String copyNumber = inputWords[2];
            String personCpr = inputWords[3];
            String borrowDate = inputWords[4];
            String period = inputWords[5];
            String state = inputWords[6];
            Person foundPerson = personUI.findPerson(personCpr);
            if(foundPerson != null) 
            {
                DVD foundDVD = dvdUI.findDVD(dvdBarcode);
                if(foundDVD != null) 
                {
                    Copy foundCopy = dvdUI.findCopy(foundDVD, copyNumber);
                    if (foundCopy != null) 
                    {
                        createLoan(foundCopy, foundPerson, id, borrowDate, period, state);
                        print = "Loan #" + id + " has been created.";
                    }
                    else 
                    {
                        print = "ERROR: Copy not found.";
                    }
                }
                else 
                {
                    print = "ERROR: DVD not found.";
                }
            }
            else 
            {
                print = "ERROR: Person not found";
            }
        }
        else 
        {
            print = "ERROR: Bad input.";
        }
        return print;
    }

    // Start "Find a Loan"
    public String loansFindStart() 
    {
        String print = null;

        System.out.print("\nEnter ID:");

        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        Loan foundLoan = findLoan(id);

        if(foundLoan != null) 
        {
            print = " # Found Loan: #" + "\n" 
            + "  Copy #: " + foundLoan.getCopy().getSerialNumber() + "\n"
            + "  Person: " + foundLoan.getPerson().getCpr() + "\n"
            + "  ID: " + foundLoan.getId() + "\n"
            + "  Borrow date: " + foundLoan.getBorrowDate() + "\n"
            + "  Period: " + foundLoan.getPeriod() + "\n"
            + "  State: " + foundLoan.getState() + "\n";
        }
        else 
        {
            print = "ERROR: Loan not found.";
        }
        return print;
    }

    // Start "Update a Loan"
    public String loansUpdateStart() 
    {
        String print = null;

        System.out.print("\nEnter ID:");

        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        Loan foundLoan = findLoan(id); 
        if(foundLoan != null) 
        {
            System.out.println("\nEnter the new loan period for Loan #" + id + " :");

            Scanner newSc = new Scanner(System.in);
            String newPeriod = newSc.nextLine();
            if(newPeriod != null) 
            {
                updateLoan(foundLoan,newPeriod);
                print = "Period of Loan #" + id + " has been updated to " + newPeriod + ".";
            }
            else 
            {
                print = "ERROR: Bad input.";
            }

        }
        else 
        {
            print = "ERROR: Loan not found";
        }
        return print;
    }

    // Start "Remove a loan"
    public String loansRemoveStart() 
    {
        String print = null;

        System.out.print("\nEnter ID: ");

        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        Loan foundLoan = findLoan(id);

        if(foundLoan != null) 
        {
            deleteLoan(foundLoan);
            print = "Loan #" + id + " has been deleted.";
        }
        else 
        {
            print = "ERROR: Loan not found";
        }
        return print;
    }

    // Start "Register return"
    public String loansReturnStart() 
    {
        String print = null;

        System.out.print("\nEnter ID: ");
        Scanner scID = new Scanner(System.in);
        String id = scID.next();

        Loan foundLoan = findLoan(id);
        if(foundLoan != null) 
        {
            System.out.print("\nEnter current date [DD-MM-YYYY]:");
            Scanner scDate = new Scanner(System.in);
            String returnDate = scDate.next();
            String borrowDate = foundLoan.getBorrowDate();
            String period = foundLoan.getPeriod();

            String returnState = checkDate(borrowDate,returnDate,period);
            
            registerReturn(foundLoan,returnState);
            
            print = "State of Loan #" + id + " has been saved as: " + returnState;
        }
        else 
        {
            print = "ERROR: Loan not found";
        }
        
        return print;
    }

    private String checkDate(String borrowDate, String returnDate, String period)
    {
        String returnState = null;
        
        int borrowDay = Integer.parseInt(borrowDate.substring(0,2));
        int borrowMonth = Integer.parseInt(borrowDate.substring(3,5));
        int borrowYear = Integer.parseInt(borrowDate.substring(6,10));
        
        int returnDay = Integer.parseInt(returnDate.substring(0,2));
        int returnMonth = Integer.parseInt(returnDate.substring(3,5));
        int returnYear = Integer.parseInt(returnDate.substring(6,10));
        
        long borrowDays = (borrowDay + borrowMonth*30 + borrowYear*365);
        long returnDays = (returnDay + returnMonth*30 + returnYear*365);
        
        long difference = returnDays - borrowDays;
        
        int periodInt = Integer.parseInt(period);
        
        if(difference > periodInt) {
            returnState = "returned after due";
        }
        else 
        {
            returnState = "returned";
        }
        
        
        return returnState;
        
            
    }
}




